<%@ page import="au.edu.educationau.opensource.dsm.adapters.*,au.edu.educationau.opensource.dsm.obj.*,au.edu.educationau.opensource.dsm.worker.*,au.edu.educationau.opensource.dsm.util.*,java.util.*" %>
<%
	SearchCriteria criteria = (SearchCriteria) request.getAttribute("criteria");
	boolean isAdvanced = StringUtils.checkString(request.getParameter("advanced"), "").length() > 0;
	boolean debugPriority = StringUtils.checkString(request.getParameter("prioritymode"), "").length() > 0;
	int rawResultCount = 0;
	int setResultCount = 0;
	int theoreticalTotal = 0;
	int pageCount = 0;
	float timeTaken = 0f;
	int pageSize = EducationAuUtils.getInteger(request.getParameter("ps"), 10, 100);
	int start = EducationAuUtils.getInteger(request.getParameter("start"), 1, 200);
	String rset = StringUtils.checkString(request.getParameter("rset"), ""); // result set by source
	boolean allowDuplicates = StringUtils.checkString(request.getParameter("dupes"), "true").equals("true");
	boolean isCompletedSet = false;

	StringBuffer sourceCollage = new StringBuffer();
	StringBuffer thesCollage = new StringBuffer();
	SearchResult[] results = null;

	if (null == criteria) {
		criteria = new SearchCriteria();
	}
	String[] sources = criteria.getSources();
	String[] prioritisedSources = new String[sources.length];
	String priority;
	boolean showInterleavingOptions = sources.length > 1 && rset.length() <= 0;
	String sortAlgorithm = StringUtils.checkString(request.getParameter("sort"), SearchResultSorter.SORTED_BY_PRIORITY_INTERLEAVED);
	if (!showInterleavingOptions && sortAlgorithm.equals(SearchResultSorter.SORTED_BY_PRIORITY_INTERLEAVED)) {
		sortAlgorithm = SearchResultSorter.SORTED_BY_RELEVANCE;
	}

	for (int i = 0; i < sources.length; i++) {
		sourceCollage.append(sources[i]);
		priority = StringUtils.checkString(request.getParameter("priority." + sources[i]), "");
		if (priority.length() <= 0) {
			if (SearchAdapterFactory.adapterExists(sources[i])) {
				priority = "" + SearchAdapterFactory.getAdapterProperties(sources[i]).getPriority();
			} else {
				priority = "1000";
			}
		}
		prioritisedSources[i] = priority + "." + sources[i];
	}

	Arrays.sort(prioritisedSources);

	String[] thesauri = criteria.getThesauri();
	for (int i = 0; i < thesauri.length; i++) {
		thesCollage.append(thesauri[i]);
	}

	SearchResultSet set = (SearchResultSet) request.getAttribute("resultset");
	if (null != set) {
		isCompletedSet = set.isCompletedSet();
		rawResultCount = set.getRawResultCount();
		if (rset.equals("")) {
			results = set.getDistributedResults(start, pageSize, criteria.getMaxResults(), sortAlgorithm, allowDuplicates, prioritisedSources);
			timeTaken = set.getTotalTimeTaken() / 1000f;
			theoreticalTotal = set.getTotalTheoreticalResultsAvailable();
		} else {
			results = set.getResultsbySource(start, pageSize, rset, sortAlgorithm);
			timeTaken = set.getTimeTakenBySource(rset) / 1000f;
			theoreticalTotal = set.getTheoreticalResultsAvailable(rset);
		}
		setResultCount = set.getSetResultCount();
		if (null != results) {
			pageCount = results.length;
		}
	}
	int pages = (int) Math.ceil((float) setResultCount / (float) pageSize);
	int currentPage = (int) Math.ceil((float) start / (float) pageSize + 0.5f);
	response.setContentType("text/html; charset=UTF-8");
%><html><head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8"><title>DSM Search: <%=criteria.getQuery()%> </title><style><!--
body,td,div,.p,a{font-family:arial,sans-serif }
div,td{color:#000}
.f,.fl:link{color:#6f6f6f}
a:link,.w,a.w:link,.w a:link{color:#00c}
a:visited,.fl:visited{color:#551a8b}
.t a:link,.t a:active,.t a:visited,.t{color:#ffffff}
.t{background-color:#3366cc}
.h{color:#3366cc}
.i,.i:link{color:#a90a08}
.a,.a:link{color:#008000}
.z{display:none}
div.n {margin-top: 1ex}
.n a{font-size:10pt; color:#000}
.n .i{font-size:10pt; font-weight:bold}
.q a:visited,.q a:link,.q a:active,.q {text-decoration: none; color: #00c;}
.b{font-size: 12pt; color:#00c; font-weight:bold}
.pr{font-size: 8pt;}
.ch{cursor:pointer;cursor:hand}
.e{margin-top: .75em; margin-bottom: .75em}
.g{margin-top: 1em; margin-bottom: 1em}
//-->
</style>
<script>
<!--
function ss(w){window.status=w;return true;}
function cs(){window.status='';}
//-->
</script>
<script>
<!--
function ga(o,e){if (document.getElementById){a=o.id.substring(1); p = "";r = "";g = e.target;if (g) { t = g.id;f = g.parentNode;if (f) {p = f.id;h = f.parentNode;if (h) r = h.id;}} else{h = e.srcElement;f = h.parentNode;if (f) p = f.id;t = h.id;}if (t==a || p==a || r==a) return true;location.href=document.getElementById(a).href}}
function np(p,t,r) {document.gs.start.value=p;document.gs.token.value=t;document.gs.rset.value=r;document.gs.submit();}
function ns(q) {document.gs.q.value=q;document.gs.submit();}
function sort(t) {document.gs.sort.value=t;document.gs.submit();}
//-->
</script>
</head>

<body bgcolor=#ffffff onLoad="document.gs.reset()">
<table border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td rowspan="2" valign="top" width="120" align="center" valign="bottom"><a href="index.html"><img src="images/dsmlogo.jpg" alt="Go to DSM Home" border="0"></a></td>
  </tr>
	<tr>
		<td valign="middle" width="700">
			<form name="gs" method="GET" action="search"><input type="text"
				name="q" size="31" maxlength="2048" value="<%=criteria.getQuery()%>"><font
				size="-1"> <input type="submit" value="DSM Search">&#160;
				<% if (isAdvanced) { %>
					<a href="/search?q=">Standard Search<a>
				<%	} else { %>
					<a href="/advancedsearch?q=">Advanced Search<a>
				<%	} %>
				<br />
				<% if (isAdvanced) { %> 
					<%@include file="advanced.jsp"%>
				<% } else {	%> 
					<%@include file="standard.jsp"%> 
				<% } %>
			</form>
		</td>
	</tr>

</table>
  <script><!--
function b(p,l,e){var re=new RegExp('/search');var f=document.gs;var a=f.action;if (a && document.getElementById) {f.action=a.replace(re,p);var hf=document.getElementById("hf");if (hf) {var t = "<input type=hidden name=sa value=N><input type=hidden name=tab value="+l+">";if (l.charAt(1) == 'd')t += "<input type=hidden name=cat value=gwd/Top>";else {var c1=document.getElementById("ct1");var c2=document.getElementById("ct2");if (c1 && c2) {c1.value="";c2.value="";}if (f.cat)f.cat.value="";}hf.innerHTML=t;}e.cancelBubble=true;f.submit();return false;}return true;}
function c(p,l,e) {b(p,l,e);}
// -->

</script>
<a name="results"></a>
<table border=0 cellpadding=0 cellspacing=0>
	<tr>
	<%
		if (rset.equals("") && null != set) {
	%>
		<td id=t0 bgcolor="#707824" width=60 nowrap>
			<center><font size=-1 color=#ffffff><b>&nbsp;Most&nbsp;Relevant&nbsp;Results&nbsp;</b></font></center>
		</td>
		<td width=1 bgcolor=#808080><img width=1 height=1 alt=""></td>
		<td width=1 bgcolor=#ffffff><img width=1 height=1 alt=""></td>
	<%
		} else if (null != set) {
	%>
		<td id=t1 class=q bgcolor=#efefef width=60 nowrap>
			<center><font size=-1><a id=t1a class=q href="javascript:np(1, '<%=criteria.getToken()%>', '');">&nbsp;Most&nbsp;Relevant&nbsp;Results&nbsp;</a></font></center>
		</td>
		<td width=1 bgcolor=#808080><img width=1 height=1 alt=""></td>
		<td width=1 bgcolor=#ffffff><img width=1 height=1 alt=""></td>
	<%
		}
		if (isCompletedSet) {
			for (int i = 0; i < sources.length; i++) {
				if (!SearchAdapterFactory.adapterExists(sources[i]))
					continue;
				if (sources[i].equals(rset)) {
	%>
			<td id=t<%=i+1%> bgcolor="#707824" width=60 nowrap>
				<center><font size=-1 color=#ffffff><b>&nbsp;<%=SearchAdapterFactory.getAdapterProperties(sources[i]).getDisplayTitle().replaceAll(" ", "&nbsp;")%>&nbsp;</b></font></center>
			</td>
		<%
			} else {
		%>
			<td id=t<%=i+1%> class=q bgcolor=#efefef width=60 nowrap>
				<center>
					<font size=-1><a id=t<%=i+1%>a class=q href="javascript:np(1, '<%=criteria.getToken()%>', '<%=sources[i]%>');">&nbsp;<%=SearchAdapterFactory.getAdapterProperties(sources[i]).getDisplayTitle().replaceAll(" ", "&nbsp;")%>&nbsp;</a></font>
				</center>
			</td>
		<%
			}
		%>
			<td width=1 bgcolor=#808080><img width=1 height=1 alt=""></td>
			<td width=1 bgcolor=#ffffff><img width=1 height=1 alt=""></td>
		<%
			}
			} else {
		%>
		<td width=1 bgcolor=#808080><img width=1 height=1 alt=""></td><%
			}
		%>
	</tr>
</table>
  <table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#707824">
    <tr>
      <td bgcolor="#707824" nowrap>
        <table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#707824">
          <tr>
            <td bgcolor="#707824" nowrap><font size="-1" color="#ffffff">Searched
               for <b><font color="#ffffff"><%=criteria.getQuery()%></font></b>.&nbsp;</font></td>
            <td bgcolor="#707824" align="right" nowrap><font size="-1" color="#ffffff">Results
              <b><%=start%></b> - <b><%=start + pageCount - 1%></b> of <b><%=setResultCount%> <%
 	if (!rset.equals("")) {
 %>(<%=theoreticalTotal%> total results)<%
 	}
 %></b>. Search took <b><%=timeTaken%></b>
              seconds.</font></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
<%
	if (thesCollage.length() > 0 && null != set) {
		String[] narrower = set.getNarrowerTerms();
		String[] like = set.getLikeTerms();
		String[] broader = set.getBroaderTerms();
%><table border=0 cellpadding=0 cellspacing=4 align=right width=20%><%
	if (narrower.length > 0) {
%><tr>
  		<td align=center><br><span class=f><font size=-1>NARROWER Terms</font></span>
  	</td></tr>
  	<tr><td>
  		<table bgcolor=#B4D0DC border=0 cellspacing=0 width=100%>
  			<tr><td>
  				<table border=0 cellpadding=3 cellspacing=0 width=100%>
  					<tr><td id=taw1 class=ch bgcolor=#ECF8FF>
  						<font class=f size=-2>
  						<%
  							for (int i = 0; i < narrower.length; i++) {
  						%><a href="javascript:ns('<%=narrower[i]%>');"><%=narrower[i]%></a><br /><%
  							}
  						%>
  						</font>
  				    </td></tr>
  				</table>
  			</td></tr>
  		</table>
  	</td></tr><%
  		}
  			if (like.length > 0) {
  	%><tr>
  		<td align=center><br><span class=f><font size=-1>LIKE Terms</font></span>
  	</td></tr>
  	<tr><td>
  		<table bgcolor=#B4D0DC border=0 cellspacing=0 width=100%>
  			<tr><td>
  				<table border=0 cellpadding=3 cellspacing=0 width=100%>
  					<tr><td id=taw1 class=ch bgcolor=#ECF8FF>
  						<font class=f size=-2>
  						<%
  							for (int i = 0; i < like.length; i++) {
  						%><a href="javascript:ns('<%=like[i]%>');"><%=like[i]%></a><br /><%
  							}
  						%>
  						</font>
  				    </td></tr>
  				</table>
  			</td></tr>
  		</table>
  	</td></tr><%
  		}
  			if (broader.length > 0) {
  	%><tr>
  		<td align=center><br><span class=f><font size=-1>BROADER Terms</font></span>
  	</td></tr>
  	<tr><td>
  		<table bgcolor=#B4D0DC border=0 cellspacing=0 width=100%>
  			<tr><td>
  				<table border=0 cellpadding=3 cellspacing=0 width=100%>
  					<tr><td id=taw1 class=ch bgcolor=#ECF8FF>
  						<font class=f size=-2>
  						<%
  							for (int i = 0; i < broader.length; i++) {
  						%><a href="javascript:ns('<%=broader[i]%>');"><%=broader[i]%></a><br /><%
  							}
  						%>
  						</font>
  				    </td></tr>
  				</table>
  			</td></tr>
  		</table>
  	</td></tr><%
  		}
  	%>
  </table>
<%
	}
%>
<div class="n" style="width: 75%;">
  <%
  	if (null != set && set.getCorrectedTerms().length() > 0) {
  %><br /><b>Did you mean </b><a href="javascript:ns('<%=set.getCorrectedTerms()%>');"><b><i><%=set.getCorrectedTerms()%></i></b></a><b>?</b><%
  	}
  %>
  <%
  	if (null != set && !isCompletedSet) {
  %><br /><center><a href="javascript:np(1, '<%=criteria.getToken()%>', '');"><b>Get More Results</b></a></center><%
  	}
  %>
  <br />
  <%
  	if (isCompletedSet && pages > 0) {
  %><center><font size="-1"><b>Result&nbsp;Page:&nbsp;</b></font><%
  	for (int i = 0; i < pages; i++) {
  			if (i + 1 == currentPage) {
  %><b><%=i + 1%></b>&#160;<%
  	} else {
  %> <a href="javascript:np(<%=(pageSize*i)+1%>,'<%=criteria.getToken()%>','<%=rset%>');"><%=i + 1%></a>&#160;<%
 	}
 		}
 %>
   <br><br>
   <div style="text-align:left"><font size="-2">
    Sort these results by:&#160;
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_PRIORITY_INTERLEAVED) && showInterleavingOptions) {
    %><b>Priority (Interleaved)</b>&#160;|&#160;<%
    	} else if (showInterleavingOptions) {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_PRIORITY_INTERLEAVED%>');"><font size="-2">Priority (Interleaved)</font></a>&#160;|&#160;<%
    	}
    %>
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_RELEVANCE)) {
    %><b>Relevance</b><%
    	} else {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_RELEVANCE%>');"><font size="-2">Relevance</font></a><%
    	}
    %>&#160;|&#160;
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_SOURCE)) {
    %><b>Source</b><%
    	} else {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_SOURCE%>');"><font size="-2">Source</font></a><%
    	}
    %>&#160;|&#160;
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_TITLE)) {
    %><b>Title</b><%
    	} else {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_TITLE%>');"><font size="-2">Title</font></a><%
    	}
    %>&#160;|&#160;
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_IDENTIFIER)) {
    %><b>Identifier</b><%
    	} else {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_IDENTIFIER%>');"><font size="-2">Identifier</font></a><%
    	}
    %>&#160;|&#160;
    <%
    	if (sortAlgorithm.equals(SearchResultSorter.SORTED_BY_DATE)) {
    %><b>Date</b><%
    	} else {
    %><a href="javascript:sort('<%=SearchResultSorter.SORTED_BY_DATE%>');"><font size="-2">Date</font></a><%
    	}
    %></font>
   </div>
<%
	}
%></center><%
	if (results != null && pageCount > 0) {
%>
    <p class="g"><!--m--><%
    	for (int i = 0; i < results.length; i++) {
    			SearchResult res = results[i];
    			String categoryURI = SearchAdapterFactory.getCategoryURI(res.getSource());
    %><a href="<%=res.getLink()%>"><%=res.getTitle()%></a>&nbsp;<a href="http://www.google.com/gwt/n?u=<%=java.net.URLEncoder.encode(res.getLink())%>">(Mobile Format)</a>
	<%
		if (res.getPreview().length() > 0) {
	%>
	  <a href="<%=res.getPreview()%>"><font size="-1" color="blue"><b>[Preview]</b></font></a>&#160;
	<%
		}
				if (res.getDownload().length() > 0) {
	%>
	  <a href="<%=res.getDownload()%>"><font size="-1" color="blue"><b>[Download]</b></font></a>&#160;
	<%
		}
				if (res.getMetadataScheme().length() > 0) {
	%>
	  <font size="-1" color="blue"><b>[<%=res.getMetadataScheme().toUpperCase()%>]</b></font>&#160;
	<%
		}
				if (res.getContentPackage().length() > 0) {
	%>
	  <font size="-1" color="blue"><b>[<%=res.getContentPackage().toUpperCase()%>]</b></font>&#160;
	<%
		}
	%>
    <br>
    <font size="-1">
    <%
    	if (!sortAlgorithm.equals(SearchResultSorter.SORTED_BY_PRIORITY_INTERLEAVED)) {
    %>
    <span class="f">Relevance:</span><%=Math.round(res.getRelevance() * 100f)%>%<br>
    <%
    	}
    %>
    <span class="f">Description:</span><%
    	if (res.getDescription().length() > 400) {
    				out.print(res.getDescription().substring(0, 400) + "...");
    			} else {
    				out.print(res.getDescription());
    			}
    %>
    <br>
    <span class="f">Date updated:</span><%
      if (res.getDate().length() > 0) {
    	    out.print(res.getDate());
		} else {
			out.print(res.getDate());
		}
    %>    
    <br><%
    	ResultCategory[] cats = res.getResultCategories();
    			if (cats != null) {
    %><span class="f">Category: </span><%
    	for (int j = 0; j < cats.length; j++) {
    					ResultCategory cat = cats[j];
    %><a class="fl" href="<%=categoryURI+cat.getPath()%>"><%=cat.getName()%></a>,<%
    	} // for
    %><br><%
    	} // if
    %>
    <%
    	if (res.getRights().length() > 0) {
    %><span class="f">Rights:</span><%=res.getRights()%><br><%
    	}
    %>
    <font color="#008000"><%=res.getLink()%></font><%
    	if (res.getDate().length() > 0) {
    %>&#160;-&#160;<%=res.getDate()%><%
    	}
    %>&#160;-&#160;[<a href="<%=SearchAdapterFactory.getAdapterProperties(res.getSource()).getWebsiteURL()%>"><%=SearchAdapterFactory.getAdapterProperties(res.getSource()).getDisplayTitle()%></a>]<br><br>
    </font><%
    	}
    %><br clear="all"><%
    	if (isCompletedSet && pages > 0) {
    %><div class="n">
    <table border="0" cellpadding="0" width="1%" cellspacing="0" align="center">
      <tr align="center" valign="top">
        <td valign="bottom" nowrap><font size="-1"><b>Result&nbsp;Page:&nbsp;</b></font><%
        	for (int i = 0; i < pages; i++) {
        %><td><%
        	if (i + 1 == currentPage) {
        %><b><%=i + 1%></b>&#160;<%
        	} else {
        %> <a href="javascript:np(<%=(pageSize*i)+1%>,'<%=criteria.getToken()%>','<%=rset%>');"><%=i + 1%></a>&#160;<%
 	}
 %></td><%
 	}
 %>
    </table>
  </div><%
  	}
  	} else {
  		if (!isCompletedSet && criteria.getSearchStrategy().equals(SMEvents.SEARCH_STRATEGY_WAIT_FOR_FASTEST) && null != set) {
  %>
		<br />
		<b>No Results found so far...</b>
	<%
		} else if (null != set) {
	%>
		<br />
		<b>No Results found.</b>
	<%
		}
		}
	%>
</div>
  <center>
    <p>&nbsp;</p>
    <font size="-2" face="Arial, sans-serif">&copy;2008 <b>education.au</b>  
    </font>
  </center>

</body></html>

